#!/usr/bin/perl -w

=head1 NAME

dh_javadoc - generate javadoc documentation and install in package directory

=cut

use strict;
use Debian::Debhelper::Dh_Lib;

=head1 SYNOPSIS

B<dh_javadoc> [S<I<debhelper options>>] [B<-p<package>>] [B<--sourcedir I<source path>>] [S<I<Java packages>>]

=head1 DESCRIPTION

dh_javadoc is a debhelper program that is responsible for generating HTML Java
documentation and installing it in package directories. This should be done in
the documentaion package of all Java libraries. Normal Java programs should not
be supplied with javadoc documentation (neither in their main package or in a
separate documentation package)

Any package specified as parameters will be installed into the first package
dh_javadoc is told to act on. Use of the standard debhelper B<-p> option is
highly recommended.

A file named debian/package.javadoc may list additional packages to have
documentation generated. These should be separated by new lines.

When the Java policy is modified, this script will also install links to the
documentation in a common place so that javadoc pages may be interlinked; and
also provide a substitution variable for documentation package dependencies.

Javadoc generation is done using gjdoc, currently the only free javadoc
implementation. When new features are added to this (such as overview files),
this script will be modified accordingly.

=head1 OPTIONS

=over 4

=item B<--sourcedir I<source path>>

Specify the source path in which to look for Java source files.

=item I<Java packages>

Names of Java packages to be documented.

=back

=head1 TODO

=over 4

=item + 

Check parameters more carefully.

=item + 

Add substvar for documentation dependencies

=item + 

Don't put the 'common' directory in all packages - refer to one in
/usr/share/gjdoc & insist on creating that dependency 

=item +

Other items depend on gjdoc improvements:

=over 8

=item -

Add package listings to common directory

=item -

Generate dependency substvar based on additional doc packages used.

=back

=back

=cut

use Cwd;

init(options => {
        "sourcedir=s" => \$dh{SOURCEDIR},
});

foreach my $package (@{$dh{DOPACKAGES}}) {
	my $tmp=tmpdir($package);

	my @packages = @ARGV;

	if (-e "debian/$package.javadoc"){
		open PACKAGES, "debian/$package.javadoc"
			or error("Unable to open file: debian/$package.javadoc: $!");
		my @pkgs;
		my $i = 0;
		while(<PACKAGES>){chomp($pkgs[$i++]=$_);}
		close PACKAGES or warning("Error closing debian/$package.javadoc: $!");
		@packages = (@packages, @pkgs);
	}
	
	my $src_dir;
	if ($dh{SOURCEDIR}){
		$src_dir = getcwd .'/'. $dh{SOURCEDIR};
	}else{
		$src_dir = getcwd();
	}

	if (! -e "/usr/bin/javadoc"){
		error("/usr/bin/javadoc not found");
	}
	
	# make directory
	doit('install', '-g', '0', '-o', '0', '-d', "$tmp/usr/share/doc/$package/api/");
	# generate javadoc
	doit('javadoc', "-sourcepath",  $src_dir, "-d",  "$tmp/usr/share/doc/$package/api/", @packages);
  # and remove gjdoc_rawcomment.cache
  doit('rm', "-f", "gjdoc_rawcomment.cache");

}

=head1 SEE ALSO

L<debhelper(7)>

This program is designed similar to debhelper

=head1 AUTHOR

Mark Howard <mh@debian.org>

=cut
